package com.example.back.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.back.common.Result;
import com.example.back.entity.ExpertGroup;
import com.example.back.entity.Experts;
import com.example.back.entity.User;
import com.example.back.service.ExpertGroupService;
import com.example.back.service.ExpertsService;
import com.example.back.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Jm
 * @date 2024/7/18
 */
@RestController
@RequestMapping("/experts")
public class ExpertsController
{
    @Autowired
    private ExpertGroupService expertGroupService;

    @Autowired
    private ExpertsService expertsService;

    @Autowired
    private UserService userService;

    @PostMapping("/addGroup")
    public Result addGroup(@RequestBody ExpertGroup expertGroup)
    {
        expertGroupService.save(expertGroup);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delGroup(@PathVariable Integer id)
    {
        expertGroupService.removeById(id);
        return Result.success();
    }

    @PostMapping("/addExpert")
    public Result addExpert(@RequestBody Experts expert)
    {
        expertsService.save(expert);
        return Result.success();
    }

    @PostMapping("delExpert")
    public Result delExpert(@RequestBody Experts experts)
    {
        QueryWrapper<Experts> queryWrapper = new QueryWrapper<Experts>();
        queryWrapper.eq("expert_id", experts.getExpertId());
        queryWrapper.eq("group_id", experts.getGroupId());

        ExpertGroup expertGroup = expertGroupService.getById(experts.getGroupId());
        int groupExpertId = expertGroup.getExpertId();
        int expertsExpertId = experts.getExpertId();
        if(groupExpertId == expertsExpertId)
        {
            expertGroup.setExpertId(null);
            expertGroup.setExpertName(null);
            expertGroupService.removeById(expertGroup.getId());
            expertGroupService.saveOrUpdate(expertGroup);
        }
        expertsService.remove(queryWrapper);
        return Result.success();
    }

    @GetMapping("/selectExpert")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize)
    {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("identity", 5);

        Page<User> page = userService.page(new Page<>(pageNum, pageSize), queryWrapper);

        return Result.success(page);
    }

    @GetMapping("/selectGroup")
    public Result selectGroup()
    {
        List<ExpertGroup> groupList = expertGroupService.list();
        for(ExpertGroup expertGroup : groupList)
        {
            QueryWrapper<Experts> expertQw = new QueryWrapper<Experts>();
            expertQw.eq("group_id", expertGroup.getId());
            List<Experts> expertsList = expertsService.list(expertQw);
            expertGroup.setExperts(expertsList);
        }
        return Result.success(groupList);
    }

    @GetMapping("/list")
    public Result selectGroupList()
    {
        return Result.success(expertGroupService.seleList());
    }

    @PostMapping("/setMaster")
    public Result setMaster(@RequestBody ExpertGroup expertGroup)
    {
        expertGroupService.saveOrUpdate(expertGroup);
        return Result.success();
    }

}
